/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package customers;

import entities.BookRooms;
import entities.BookTables;
import entities.Customers;
import entities.OderFood;
import entities.OrderFoodDetails;
import entities.Roles;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.ejb.EJB;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.transaction.UserTransaction;
import session.BookRoomsFacadeLocal;
import session.BookTablesFacadeLocal;
import session.CustomersFacadeLocal;
import session.OrderFoodDetailsFacadeLocal;
import session.RolesFacadeLocal;
import session.ShowAllOrderLocal;

/**
 *
 * @author TANGQUOCTUAN
 */
public class ControllerCustomer extends HttpServlet {

    @EJB
    private OrderFoodDetailsFacadeLocal orderFoodDetailsFacade;
    @EJB
    private BookTablesFacadeLocal bookTablesFacade;
    @EJB
    private BookRoomsFacadeLocal bookRoomsFacade;
    @EJB
    private RolesFacadeLocal rolesFacade;
    @EJB
    private CustomersFacadeLocal customersFacade;

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession(true);
        try {
            String action = request.getParameter("action");

            if (action.equals("Login")) {
                String user = request.getParameter("txtUser");
                String pass = request.getParameter("txtPass");
                String result = this.customersFacade.checkLogin(user, pass);
                if (result.equals("admin")) {
                    session.setAttribute("user", user);
                    session.setAttribute("isadmin", user);
                    request.getRequestDispatcher("successlogin.jsp").forward(request, response);
                } else if (result.equals("customers")) {
                    session.setAttribute("user", user);
                    request.getRequestDispatcher("successlogin.jsp").forward(request, response);
                } else if (result.equals("banned")) {
                    request.getRequestDispatcher("errorbanned.jsp").forward(request, response);
                } else if (result.equals("")) {
                    response.sendRedirect("errorlogin.jsp");
                }
            } else if (action.equals("LogOut")) {
                session.invalidate();
                response.sendRedirect("index.jsp");
            } else if (action.equals("Register")) {
                String users = request.getParameter("txtUserRig");
                Customers findcus = customersFacade.find(users);
                if (findcus != null) {
                    response.sendRedirect("error_register.jsp");
                } else {
                    String user = request.getParameter("txtUserRig").trim();
                    String pass = request.getParameter("txtCPassRig").trim();
                    String fname = request.getParameter("txtFnameRig").trim();
                    String lname = request.getParameter("txtLnameRig").trim();
                    String email = request.getParameter("txtEmailRig").trim();
                    String phone = request.getParameter("txtPhoneRig").trim();
                    String address = request.getParameter("txtAddressRig").trim();
                    Boolean result = this.customersFacade.insertCus(user, pass, fname, lname, email, phone, address, 2);
                    if (result) {
                        session.setAttribute("user", user);
                        request.getRequestDispatcher("successregis.jsp").forward(request, response);
                    }
                }
            } else if (action.equals("Information")) {
                ServletContext context = getServletContext();
                String user = request.getParameter("user");
                List A = this.customersFacade.showByUser(user);
                context.setAttribute("info", A);
                request.getRequestDispatcher("custinformation.jsp").forward(request, response);
            } else if (action.equals("Edit")) {
                String user = request.getParameter("txtUserIf");
                ServletContext context = getServletContext();
                List A = this.customersFacade.showByUser(user);
                context.setAttribute("info", A);
                request.getRequestDispatcher("editcustinformation.jsp").forward(request, response);
            } else if (action.equals("Update")) {
                ServletContext context = getServletContext();
                String user = request.getParameter("txtUserIf").trim();
                String pass = request.getParameter("txtPassIf").trim();
                String fname = request.getParameter("txtFnameIf").trim();
                String lname = request.getParameter("txtLnameIf").trim();
                String email = request.getParameter("txtEmailIf").trim();
                String phone = request.getParameter("txtPhoneIf").trim();
                String address = request.getParameter("txtAddressIf").trim();
                this.customersFacade.updateCus(user, pass, fname, lname, email, phone, address);
                List A = this.customersFacade.showByUser(user);
                context.setAttribute("info", A);
                session.setAttribute("user", user);
                request.getRequestDispatcher("updatesuccess.jsp").forward(request, response);
            } else if (action.equals("Customer")) {
                ServletContext context = getServletContext();
                List cus = this.customersFacade.showAllCustomer();
                context.setAttribute("listCus", cus);
                request.getRequestDispatcher("cusInformation_admin.jsp").forward(request, response);
            } else if (action.equals("Search")) {
                ServletContext context = getServletContext();
                String user = request.getParameter("txtSearch");
                List A = customersFacade.searchCust(user);
                context.setAttribute("listCus", A);
                session.setAttribute("old", user);
                request.getRequestDispatcher("cusInformation_admin.jsp").forward(request, response);
            } else if (action.equals("Edits")) {
                String user = request.getParameter("id");
                Customers cuss = customersFacade.find(user);
                List<Roles> role = rolesFacade.findAll();
                request.setAttribute("info", cuss);
                request.setAttribute("role", role);
                request.getRequestDispatcher("editCusInforAdmin.jsp").forward(request, response);
            } else if (action.equals("Updates")) {
                String user = request.getParameter("txtUserIf").trim();
                String pass = request.getParameter("txtPassIf").trim();
                String fname = request.getParameter("txtFnameIf").trim();
                String lname = request.getParameter("txtLnameIf").trim();
                String email = request.getParameter("txtEmailIf").trim();
                String phone = request.getParameter("txtPhoneIf").trim();
                String address = request.getParameter("txtAddressIf").trim();
                Integer role = new Integer(request.getParameter("drop"));
                Boolean result = this.customersFacade.updateCusAdmin(user, pass, fname, lname, email, phone, address, role);
                if (result) {
                    response.sendRedirect("updatesuccessadmin.jsp");
                }
            } else if (action.equals("changepass")) {
                String user = request.getParameter("id");
                ServletContext context = getServletContext();
                List A = this.customersFacade.showByUser(user);
                context.setAttribute("info", A);
                request.getRequestDispatcher("changepass.jsp").forward(request, response);
            } else if (action.equals("Change")) {
                String pass = request.getParameter("txtCpass");
                String user = request.getParameter("id");
                Boolean result = this.customersFacade.changePass(user, pass);
                if (result) {
                    session.invalidate();
                    response.sendRedirect("changesuccess.jsp");
                }
            } else if (action.equals("UBookroom")) {
                String user = request.getParameter("user");
                List<BookRooms> A = bookRoomsFacade.showallbyID(user);
                if (A.size() == 0) {
                    response.sendRedirect("errorcontent.jsp");
                }
                request.setAttribute("info", A);
                request.getRequestDispatcher("allBookRoom.jsp").forward(request, response);
            } else if (action.equals("UBooktable")) {
                String user = request.getParameter("user");
                List<BookTables> A = bookTablesFacade.showallbyID(user);
                if (A.size() == 0) {
                    response.sendRedirect("errorcontent.jsp");
                }
                request.setAttribute("info", A);
                request.getRequestDispatcher("allBookTable.jsp").forward(request, response);
            } else if (action.equals("UOrderfood")) {
                String user = request.getParameter("user");
                Context context = new InitialContext();
                UserTransaction utx = (UserTransaction) context.lookup("UserTransaction");
                ShowAllOrderLocal local = (ShowAllOrderLocal) context.lookup("ShowAllOrderJNDI");
                utx.begin();
                List<OderFood> listoder = local.getAllOrderFood(user);
                if (listoder.size() == 0) {
                    response.sendRedirect("errorcontent.jsp");
                }
                request.setAttribute("ALLORDER", listoder);
                request.getRequestDispatcher("allOrderfood.jsp").forward(request, response);
                utx.commit();
            } else if (action.equals("Details")) {
                Integer id = new Integer(request.getParameter("id"));
                List<OrderFoodDetails> A = orderFoodDetailsFacade.showallbyID(id);
                request.setAttribute("info", A);
                request.getRequestDispatcher("details_Orderfood.jsp").forward(request, response);
            } else if (action.equals("SearchByUser")) {
                int role = 2;
                String name = request.getParameter("name");
                List<Customers> listCus = customersFacade.showByUserReport(name, role);
                System.out.println(listCus.size());
                request.setAttribute("info", listCus);
                request.getRequestDispatcher("report.jsp").forward(request, response);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
